package com.ssm.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.sfzy.entity.Course;
import com.ssm.sfzy.entity.Teacher;
import com.ssm.sfzy.mapper.CourseMapper;
import com.ssm.sfzy.mapper.TeacherMapper;
import com.ssm.sfzy.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jitwxs
 * @date 2024年03月30日 11:03
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public void add(Course entity) {
        courseMapper.insert(entity);
    }

    @Override
    public void edit(Course entity) {
        courseMapper.update(entity);
    }

    @Override
    public void remove(Integer id) {
        courseMapper.delete(id);
    }

    @Override
    public void removeBatch(Integer[] ids) {
        courseMapper.deleteBatch(ids);
    }

    @Override
    public List<Course> find(Course entity) {
        return courseMapper.select(entity);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, Course entity) {
        PageHelper.startPage(pageNum,pageSize);
        List<Course> list = courseMapper.select(entity);
        for (Course course : list){
            Integer teacherId = course.getTeacherId();
            Teacher teacher = teacherMapper.selectById(teacherId);
            String name = teacher.getName();
            course.setTeacherName(name);
        }

        PageInfo<Course> pageInfo = new PageInfo<>(list);
        // 整合处理
        Map<String,Object> map = new HashMap<>();
        map.put("list",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    @Override
    public Course findById(Integer id) {
        return courseMapper.selectById(id);
    }

    @Override
    public List<Course> getByTeacherId(Integer teacherId) {
        List<Course> courseList = courseMapper.getByTeacherId(teacherId);
        return courseList;
    }
}
